import {get} from "lodash-es";
import {tparam, tt} from "@kordar-lib/i18n";

export function fetchConfigOptionsWithList (fetchConfig, key, pageConfig) {
    let arr = get(fetchConfig, key, []);
    if (arr.length === 0) {
        return [];
    }
    return arr.map(item => ({"value": item, "text": tparam(pageConfig, item+".text", item)}))
}

export async function downloadWithA(send, params, name) {
    try {
        const response = await send(params);

        // 创建 Blob 对象
        const blob = new Blob([response.data]);
        const downloadUrl = window.URL.createObjectURL(blob);

        // 创建链接并触发下载
        const link = document.createElement('a');
        link.href = downloadUrl;

        // 设置文件名（可以从 headers 中取，或后端传）
        link.download = name;
        document.body.appendChild(link);
        link.click();
        link.remove();
    } catch (err) {
        console.error('下载失败:', err);
    }
}